/****************************************************
 * @class RoleUserSettingWindow
 * @extends Ext.Window
 ***************************************************/
RoleUserSettingWindow = Ext.extend(Ext.Window,{
	roleUserSettingPanel: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.roleUserSettingPanel = new RoleUserSettingPanel({id: "pnl_UserRoleSetting"});
		
		RoleUserSettingWindow.superclass.constructor.call(this, {
			layout: 'form',
			width: 640,
			height: 480,
			modal: true,
			plain: true,
			closeAction: 'hide',
			buttonAlign: 'center',
			resizable: false,
			layout: 'fit',
			items: [this.roleUserSettingPanel],
			buttons: [{
			    text: "确定",
			    scope: this,
			    disabled: false,
			    handler: function(){
			    	var reCosigners = Ext.fly("_collectUsers").dom.value;
			    	
			    	var projectTree = Ext.getCmp("combo_project_tree").comboBoxGrid;
					var node = projectTree.getSelectionModel().getSelectedNode();
					var projectId = node.id;
			    	
			    	var gpProjectMember = Ext.getCmp("gpMemberMaintenance");
			    	var record = gpProjectMember.getSelectionModel().getSelected();
			    	var roleId = record.get("role_id");
			    	
			    	var saveMask = new Ext.LoadMask(Ext.getBody(), {
						msg: "信息保存中，请稍后...",
						removeMask: true
					});
					saveMask.show();
			    	
			    	var win_RoleUserSetting = Ext.getCmp("win_RoleUserSetting");
					win_RoleUserSetting.hide();
			    	
			    	Ext.Ajax.request({
						url: '/pdm/category/setProjectRoleUser.htm',
						method: 'post',
						params: {
							userList: reCosigners,
							roleId: roleId,
							projectId: projectId
						},
						success: function(response,options){		
							//var result = Ext.decode(response.responseText).message;
							//Ext.Msg.alert("信息:", result);
							
							gpProjectMember.reload(projectId);
							
							saveMask.hide();
						},
						failure: function(response){					
							var result = Ext.decode(response.responseText).message;
							Ext.Msg.alert("信息:", result);
							this.hide();
						}
					});
			    	
			    }
			},{
			    text: "关闭",
			    disabled: false,
			    scope: this,
			    handler: function(){
			    	this.hide();
			    }
			}]
		});
	},
	reset: function(){
		this.roleUserSettingPanel.reset();
	},
	reDoLayout: function(){
		var tbpnl_usrs = Ext.getCmp("tbpnl_roleUsers");
		tbpnl_usrs.setActiveTab(0);
		this.roleUserSettingPanel.tree.root.reload(true,true);
		
		//this.roleUserSettingPanel.fuzzySearch.condition.query = "";
		//this.roleUserSettingPanel.fuzzySearch.store.reload();
	}
});

/****************************************************
 * @class RoleUserSettingPanel
 * @extends Ext.FormPanel
 ***************************************************/
RoleUserSettingPanel = Ext.extend(Ext.FormPanel,{
	tree: null,
	fuzzySearch: null,
	roleUserSelect: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.tree = new UserRoleOrgTree({id: "tp_UserRoleOrgTree"});
		this.roleUserSelect = new RoleUserSelect({id: "gp_RoleUserSelect"});
		this.fuzzySearch = new RoleUserFuzzySearchPanel({id: "gp_RoleUserFuzzySearch"});
		
		RoleUserSettingPanel.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			width: 500,
			height: 400,
			layout: 'column',
			viewConfig: {forceFit: true},
			buttonAlign: 'center',
			items: [{
				columnWidth: 0.59,
				layout: 'fit',
				border: false,
				height: 410,
				items:[{
					id: 'tbpnl_roleUsers',
		            xtype: 'tabpanel',
		            enableTabScroll: true,
					activeTab: 0,
					items:[{
						title: "Org",
						id: 'tab_userRole_OrgTree',
						iconCls: 'cls_org_tab',
						border: false,
						layout: 'fit',
						height: 400,
						items: [this.tree]
					},{
						title: "Search",
						id: 'tab_userRole_SearchPanel',
						iconCls: 'cls_search_tab',
						layout: 'fit',
						border: false,
						height: 400,
						items: [this.fuzzySearch]
					}]
		        }]
			},{
				columnWidth: 0.01,
				border: false,
				height: 410,
				layout: 'fit'
			},{
				columnWidth: 0.40,
				height: 410,
				layout: 'fit',
				items:[this.roleUserSelect]
			}]
		});
	},
	reset: function(){
		this.roleUserSelect.reload();
	}
});

/****************************************************
 * @class UserRoleOrgTree
 * @extends Ext.tree.TreePanel
 ***************************************************/
UserRoleOrgTree = Ext.extend(Ext.tree.TreePanel,{
	_cur_user: null,
	_collectReCosign: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		UserRoleOrgTree.superclass.constructor.call(this, {
			autoScroll: true,
			border: false,
			width: 200,
			height: 300,
			rootVisible: false,
			singleExpand: true,
			containerScroll: true,
			loader: new Ext.tree.TreeLoader({dataUrl: '/pdm/hr/org.htm'}),
			root: new Ext.tree.AsyncTreeNode({	
			   id: '10000000',
			   text: "IVO 龙腾光电",
			   draggable: false
			})
		});
		
		this.on('dblclick', function(node){
			if(node.leaf==true){
				/*
				if(node.id == this._cur_user)
				{
					Ext.Msg.alert("温馨提示：", "您不能选择自己！");
					return ;
				}
				*/
				var collectReCosign = Ext.fly("_collectUsers").dom.value;
				var gp_RoleUserSelect = Ext.getCmp("gp_RoleUserSelect");
				
				var userStore = gp_RoleUserSelect.getStore();
				var num = userStore.getCount();
				var repeat = false; //判断人员是否重复选择
				
				if(num == 0){
					if(collectReCosign == "" ) collectReCosign = node.id;
					else collectReCosign = collectReCosign + "," + node.id;
					Ext.fly("_collectUsers").dom.value = collectReCosign;
			
					gp_RoleUserSelect.setReCosigners(collectReCosign);
					gp_RoleUserSelect.load();
				}else{
					for(var i=0;i<userStore.getCount();i++)
					{
						var record = userStore.getAt(i);
						if( record.get("employee_ID") == node.id)
						{
							repeat = true;
						}
						if(repeat) break;
					}
					
					if(repeat){
						Ext.Msg.alert("提示：","["+node.text+"] 已选择！");
					}else{
						if(collectReCosign == "" ) collectReCosign = node.id;
						else collectReCosign = collectReCosign + "," + node.id;
						Ext.fly("_collectUsers").dom.value = collectReCosign;
				
						gp_RoleUserSelect.setReCosigners(collectReCosign);
						gp_RoleUserSelect.load();
					}
				}
			}
			else Ext.Msg.alert("温馨提示：", "请您选择人员！");
		});
	}
});

/****************************************************
 * @class RoleUserSelect
 * @extends Ext.grid.GridPanel
 ***************************************************/
RoleUserSelect = Ext.extend(Ext.grid.GridPanel,{
	collectReCosign: null,
	condition: null,
	cm: null,
	store: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"员工工号", dataIndex: 'employee_ID', width:100, sortable:true},
			{header:"员工名称", dataIndex: 'employeeName', width:150}
		]);
		
		this.condition = {};
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/hr/getReCosignerList.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'employee_ID'
			},[
				{name: 'employee_ID'},
		        {name: 'employeeName'}
			])
		});
				
		RoleUserSelect.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: ['->',{
				text: "清空",
				iconCls: 'cls_clean_all',
				scope: this,
				handler: function(){
					Ext.fly("_collectUsers").dom.value = "";
					
					this.reload();
				}
			}]
		});
		
		this.on('dblclick', function(grid, rowIndex, e){
			var row = this.getSelectionModel().getSelected();
			var collectReCosign = Ext.fly("_collectUsers").dom.value;
			
			var collectReCosigns = collectReCosign.split(",");
			var j = collectReCosigns.length;
			for(var i=0;i < j;i++)
			{
				if(collectReCosigns[i] == row.get("employee_ID"))
				{
					for(var t=i;t<j;t++)
					{
						collectReCosigns[t]=collectReCosigns[t+1];
					}
					j=j-1;
				}
			}
			if(j==0) collectReCosign = "";
			else
			{
				for(var m=0;m<j;m++)
				{
					if(m==0) collectReCosign=collectReCosigns[m];
					else collectReCosign = collectReCosign+","+collectReCosigns[m];
				}
			}
			Ext.fly("_collectUsers").dom.value = collectReCosign;
			this.condition.reCosignerList = collectReCosign;
			this.store.load();
		}); 
	},
	load: function() {
		this.store.load();
	},
	setReCosigners: function(c) {
		this.condition.reCosignerList = c;
	},
	reload: function(){
		this.condition.reCosignerList = "";
		this.store.load();
	}
});

/****************************************************
 * @class UserFuzzySearchPanel
 * @extends Ext.grid.GridPanel
 ***************************************************/
RoleUserFuzzySearchPanel = Ext.extend(Ext.grid.GridPanel,{
	condition: null,
	cm: null,
	store: null,
	searchField: null,
	pagingToolbar: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"姓名", dataIndex:'employeeName', width:120},
			{header:"英文名", dataIndex:'employeeName_EN', width:150},
			{header:"部门", dataIndex:'deptName', width:150}
		]);
		
		this.condition = {};
		this.condition.limit = 50;
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/hr/getEmployeeList.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'employee_ID'
			},[
				{name: 'employee_ID'},
		        {name: 'employeeName'},
		        {name: 'employeeName_EN'},
		        {name: 'deptName'}
			])
		});
		//this.store.load();
		this.searchField = new Ext.ux.form.SearchField({store: this.store,width:200});
		
		this.pagingToolbar = new Ext.PagingToolbar({
			pageSize: 50,
			store: this.store,
			displayInfo: true,
			displayMsg: "第{0}~{1}条,共{2}",
			emptyMsg: "没有记录",
			refreshText: "刷新",
			beforePageText: "第",
			afterPageText: "页,共{0}页",
			firstText: "第一页",
			prevText: "上一页",
			nextText: "下一页",
			lastText: "最后一页"
		})
		
		RoleUserFuzzySearchPanel.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: [this.searchField],
			bbar: this.pagingToolbar
		});
		
		this.on('dblclick', function(rowIdx, r){
			var collectReCosign = Ext.fly("_collectUsers").dom.value;
			
			var gp_RoleUserSelect = Ext.getCmp("gp_RoleUserSelect");
			var userStore = gp_RoleUserSelect.getStore();
			var num = userStore.getCount();
			var repeat = false; //判断人员是否重复选择
			
			var row = this.getSelectionModel().getSelected();
			
			if(num == 0){
				if(collectReCosign == "" ) collectReCosign = row.get("employee_ID");
				else collectReCosign = collectReCosign + "," + row.get("employee_ID");
				Ext.fly("_collectUsers").dom.value = collectReCosign;
			
				gp_RoleUserSelect.setReCosigners(collectReCosign);
				gp_RoleUserSelect.load();
			}else{
				for(var i=0;i<userStore.getCount();i++)
				{
					var record = userStore.getAt(i);
					if( record.get("employee_ID") == row.get("employee_ID"))
					{
						repeat = true;
					}
					if(repeat) break;
				}
				if(repeat){
					Ext.Msg.alert("提示：","["+row.get("employeeName")+"] 已选择！");
				}else{
					if( collectReCosign == "" ) collectReCosign = row.get("employee_ID");
					else collectReCosign = collectReCosign + "," + row.get("employee_ID");
					
					Ext.fly("_collectUsers").dom.value = collectReCosign;
					
					var selectReCosign = Ext.getCmp("gp_RoleUserSelect");
					selectReCosign.setReCosigners(collectReCosign);
					selectReCosign.load();
				}
			}
			
		});
	}
});